﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;

namespace SPErrorHandler
{
    class ErrorHandler
    {
        #region Get AppSettings
        string fileErrorLogPath = ConfigurationManager.AppSettings["ErrorLogFile"];
        string fileLogPath = ConfigurationManager.AppSettings["WarmUpLogFile"];
        #endregion

        //public const string fileErrorPath = "ErrorLog.txt";
        //public const string fileLogPath = "WarmUpLog.txt";

        public void WriteErrorToFile(string argError)
        {
            try
            {
                //if fileErrorLogPath is null, set log file to default parameter ErrorLog.txt
                if (string.IsNullOrEmpty(fileErrorLogPath)) fileErrorLogPath = "ErrorLog.txt";
                TextWriter tw = new StreamWriter(fileErrorLogPath, true);
                tw.WriteLine(argError);
                tw.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
        }

        public void WriteLogToFile(string argLog)
        {
            try
            {
                //if fileLogPath is null, set log file to default parameter WarmUpLog.txt
                if (string.IsNullOrEmpty(fileLogPath)) fileLogPath = "WarmUpLog.txt";
                TextWriter tw = new StreamWriter(fileLogPath, true);
                tw.WriteLine(argLog);
                tw.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
        }

        public void DeleteFile(string filePath)
        {
            try
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
            catch (ArgumentException copyError)
            {
                ErrorHandler errHandler = new ErrorHandler();
                errHandler.WriteErrorToFile(copyError.ToString());
            }
            catch (DirectoryNotFoundException copyError)
            {
                ErrorHandler errHandler = new ErrorHandler();
                errHandler.WriteErrorToFile(copyError.ToString());
            }
            catch (NotSupportedException copyError)
            {
                ErrorHandler errHandler = new ErrorHandler();
                errHandler.WriteErrorToFile(copyError.ToString());
            }
            catch (PathTooLongException copyError)
            {
                ErrorHandler errHandler = new ErrorHandler();
                errHandler.WriteErrorToFile(copyError.ToString());
            }
            catch (UnauthorizedAccessException copyError)
            {
                ErrorHandler errHandler = new ErrorHandler();
                errHandler.WriteErrorToFile(copyError.ToString());
            }
        }
    }
}
